// pages/inviteUsers/inviteUsers.js
const app = getApp()
var utils = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        curHdIndex: 0,
        userPage: 1,
        storePage: 1,
        tishi_title: '正在加载中...',
        user_list: [],
        store_list: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        that.getUserList();
        that.getStoreList();
    },
    //tab切换
    tab: function(e) {
        this.setData({
            curHdIndex: e.currentTarget.id
        })
    },
    getUserList: function(e) {
        var that = this;
        utils.showLoading();
        var apiUrl = '/api/v1/member/my_invitation_user';
        var apiData = {
            page: that.data.userPage,
        }
        utils.http(apiUrl, apiData, function(res) {
            if (res.state == 1) {
                that.setData({
                    user_count: res.datas.count
                });
                var user_list = that.data.user_list;
                if (user_list == undefined) {
                    user_list = [];
                }
                for (var i in res.datas.list) {
                    user_list.push(res.datas.list[i])
                }
                if (res.datas.list.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '正在加载中...',
                    });
                }
                that.setData({
                    user_list: user_list,
                    user_ready: res.datas.list.length == 0 ? 1 : 2,
                })
                utils.hideLoading();
            } else {
                utils.hideLoading();
                utils.message(res.datas)
            }
        });
    },
    getStoreList: function(e) {
        var that = this;
        utils.showLoading();
        var apiUrl = '/api/v1/member/my_invitation_store';
        var apiData = {
            page: that.data.storePage,
        }
        utils.http(apiUrl, apiData, function(res) {
            if (res.state == 1) {
                that.setData({
                    store_count: res.datas.count
                });
                var store_list = that.data.store_list;
                if (store_list == undefined) {
                    store_list = [];
                }
                for (var i in res.datas.list) {
                    store_list.push(res.datas.list[i])
                }
                if (res.datas.list.length < 10) {
                    that.setData({
                        tishi_title: '已加载全部',
                    });
                } else {
                    that.setData({
                        tishi_title: '正在加载中...',
                    });
                }
                that.setData({
                    store_list: store_list,
                    store_ready: res.datas.list.length == 0 ? 1 : 2,
                })
                utils.hideLoading();
            } else {
                utils.hideLoading();
                utils.message(res.datas)
            }
        }, );
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        var that = this;
        that.setData({
            userPage: 1,
            storePage: 1,
            tishi_title: '正在加载中...',
            user_list: [],
            store_list: [],
        })
        that.getUserList();
        that.getStoreList();
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        var that = this;

        if (that.data.curHdIndex == 0) {
            if (that.data.user_ready == 2) {
                var page = that.data.userPage + 1
                that.setData({
                    userPage: page,
					storePage:1,
					store_list:[]
                })
                that.getUserList();
            }
        } else {
            if (that.data.user_ready == 2) {
                var page = that.data.storePage + 1
                that.setData({
                    storePage: page,
					userPage: 1,
					user_list:[]
                })
				that.getStoreList();
            }
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})